package com.sky.controller.admin;


import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.SetmealDish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相關接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    @ApiOperation("新增套餐")
    @PostMapping
    public Result<PageResult> addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);

        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }


    /**
     * 套餐分頁查詢
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分頁查詢")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分頁查詢：{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 批量刪除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true) // 清理所有緩存數據
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量刪除套餐：{}", ids);
        setmealService.deletByIds(ids);
        return Result.success();
    }

    /**
     * 禁用啓用員工
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("禁用啓用套倉")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@PathVariable("status") Integer status,  Long id) {
        log.info("禁用啓用套餐：{}, {}", status, id );
        setmealService.StartOrStop(status, id);

        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true) // 清理所有緩存數據
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套倉：{}", setmealDTO);

        setmealService.update(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result select(@PathVariable Long id) {
        log.info("id查詢套倉: {}", id);
        SetmealVO setmealVO = setmealService.select(id);

        return Result.success(setmealVO);
    }
}